package com.tf8.treasure.service.impl;

import com.tf8.treasure.entity.DevicePhone;
import com.tf8.treasure.entity.DevicePhoneExample;
import com.tf8.treasure.mapper.DevicePhoneMapper;
import com.tf8.treasure.service.DevicePhoneService;
import com.tf8.treasure.utils.UuidTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class DevicePhoneServiceImpl implements DevicePhoneService {

    @Autowired
    private DevicePhoneMapper devicePhoneMapper;

    @Override
    public void saveOrUpdate(DevicePhone devicePhone) {
        DevicePhoneExample example = new DevicePhoneExample();
        example.createCriteria().andMobileIdEqualTo(devicePhone.getMobileId());
        List<DevicePhone> devicePhones = devicePhoneMapper.selectByExample(example);
        devicePhone.setUpdateTime(new Date());
        if(CollectionUtils.isEmpty(devicePhones)){
            devicePhone.setDeviceNumber(UuidTools.getUuid());
            devicePhone.setCreateTime(new Date());
            devicePhoneMapper.insertSelective(devicePhone);
            return;
        }
        devicePhoneMapper.updateByExampleSelective(devicePhone,example);
    }
}
